{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "PATH_PROCESSED_DATA = 'processed/'\n",
    "FILENAME_TESTSET = PATH_PROCESSED_DATA+'test_set_combined.csv'\n",
    "FILENAME_TRAIN_AND_VALID = PATH_PROCESSED_DATA+'train_valid_set_combined_139.csv'\n",
    "PATH_TESTSET_PREDICTIONS = 'predict_tables/'\n",
    "\n",
    "#训练集的特征，处理为历史特征\n",
    "\n",
    "window_size=3\n",
    "num_forecast_steps=10\n",
    "start=60\n",
    "end=80\n",
    "\n",
    "# 读取原始数据文件\n",
    "df_old = pd.read_csv(FILENAME_TRAIN_AND_VALID)\n",
    "\n",
    "df_new = pd.DataFrame()\n",
    "\n",
    "product_pids = df_old['product_pid'].unique()\n",
    "\n",
    "for pid in product_pids[start:end]:\n",
    "    # 提取特定product_pid的数据\n",
    "    df_pid = df_old[df_old['product_pid'] == pid].reset_index(drop=True)\n",
    "    # 计算窗口数量\n",
    "    num_window = len(df_pid) - window_size\n",
    "    # 对于每个窗口的起始行\n",
    "    for start_row_of_window in range(num_window):\n",
    "        sample = {}\n",
    "         # 对于每个step    \n",
    "        for step in range(1, num_forecast_steps+1):\n",
    "            # 跳过无法生成完整step数的样本\n",
    "            if start_row_of_window+window_size-1+step >= len(df_pid):\n",
    "                continue\n",
    "            sample['apply_amt'] = df_pid['apply_amt'].iloc[start_row_of_window+window_size-1+step]\n",
    "            sample['redeem_amt'] = df_pid['redeem_amt'].iloc[start_row_of_window+window_size-1+step]\n",
    "            sample['net_in_amt'] = df_pid['net_in_amt'].iloc[start_row_of_window+window_size-1+step]\n",
    " \n",
    "            sample['product_pid'] = pid\n",
    "\n",
    "            sample['forecast_step'] = step\n",
    "            sample['transaction_date'] = df_pid['transaction_date'].iloc[start_row_of_window+window_size-1+step]\n",
    "            sample['is_week_end'] = df_pid['is_week_end'].iloc[start_row_of_window+window_size-1+step]\n",
    "            sample['is_month_end'] = df_pid['is_month_end'].iloc[start_row_of_window+window_size-1+step]\n",
    "            sample['num_days_from_base'] = df_pid['num_days_from_base'].iloc[start_row_of_window+window_size-1+step]\n",
    "\n",
    "            # 添加历史特征\n",
    "            for feature in ['apply_amt', 'redeem_amt', 'net_in_amt', 'uv_fundown','uv_stableown','uv_fundopt','uv_fundmarket','uv_termmarket', 'total_net_value', 'yield']:\n",
    "                for rank_past_day in range(1, window_size+1):\n",
    "                    sample[feature + '_' + str(rank_past_day)] = df_pid[feature].iloc[start_row_of_window+rank_past_day-1]\n",
    "            \n",
    "            df_new = pd.concat([df_new, pd.DataFrame(sample, index=[0])], ignore_index=True)\n",
    "# 将数据保存为CSV文件\n",
    "df_new.to_csv(PATH_PROCESSED_DATA+f'data_past{window_size}_train_and_valid_{start}to{end}.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "AFAC2023ApplyAndRedeem_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
